package com.xiaoxi.exam.utils;

import cn.xinzhili.xutils.core.ErrorCode;
import cn.xinzhili.xutils.core.FailureException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;


public class SmsUtil {

  private static final String product = "Dysmsapi";
  private static final String domain = "dysmsapi.aliyuncs.com";

  private static final String accessKeyId = "LTAIaQ6lkn4PmyG2";
  private static final String accessKeySecret = "4gntbvmhgGZIxlVLHxzrojRRRvW2SQ";

  public static void sendSms(String mobile, String template, String jsonStr)
      throws ClientException {
    System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
    System.setProperty("sun.net.client.defaultReadTimeout", "10000");

    IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
    DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
    IAcsClient acsClient = new DefaultAcsClient(profile);

    SendSmsRequest request = new SendSmsRequest();
    request.setPhoneNumbers(mobile);
    request.setSignName("小西考位代报");
    request.setTemplateCode(template);
    request.setTemplateParam(jsonStr);

    try {
      acsClient.getAcsResponse(request);
    } catch (Exception e) {
      throw new FailureException(ErrorCode.REQUEST_FAILED);
    }
  }
}
